package com.lgs.app.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.lgs.app.dialog.ConfirmDialogFragment;

/**
 * Created by lgs on 2018/4/20.
 */

public class UpdateAppUtil {

    public static final int CHECK_BY_VERSION_NAME = 1001;
    public static final int CHECK_BY_VERSION_CODE = 1002;
    public static final int DOWNLOAD_BY_APP = 1003;
    public static final int DOWNLOAD_BY_BROWSER = 1004;

    private Activity activity;
    private int checkBy = CHECK_BY_VERSION_CODE;
    private int downloadBy = DOWNLOAD_BY_APP;
    private String apkPath = "";
    private int serverVersionCode = 0;
    private String serverVersionName = "";
    private boolean isForce = false; //是否强制更新
    private int localVersionCode = 0;
    private String localVersionName = "";
    private String updateInfo = "";

    public static boolean showNotification = true;
    public static boolean needFitAndroidN = true; //提供给 整个工程不需要适配到7.0的项目 置为false

   /* @Deprecated
    public UpdateAppUtil needFitAndroidN(boolean needFitAndroidN) {
        UpdateAppUtil.needFitAndroidN = needFitAndroidN;
        return this;
    }*/

    private UpdateAppUtil(Activity activity) {
        this.activity = activity;
        getAppLocalVersion(activity);
    }

    public static UpdateAppUtil newInstance(Activity activity) {
        return new UpdateAppUtil(activity);
    }

    public UpdateAppUtil checkBy(int checkBy) {
        this.checkBy = checkBy;
        return this;
    }

    public UpdateAppUtil apkPath(String apkPath) {
        this.apkPath = apkPath;
        return this;
    }

    public UpdateAppUtil downloadBy(int downloadBy) {
        this.downloadBy = downloadBy;
        return this;
    }

    public UpdateAppUtil showNotification(boolean showNotification) {
        UpdateAppUtil.showNotification = showNotification;
        return this;
    }

    public UpdateAppUtil updateAppUtil(String updateInfo) {
        this.updateInfo = updateInfo;
        return this;
    }

    public UpdateAppUtil serverVersionCode(int serverVersionCode) {
        this.serverVersionCode = serverVersionCode;
        return this;
    }

    public UpdateAppUtil serverVersionName(String serverVersionName) {
        this.serverVersionName = serverVersionName;
        return this;
    }

    public UpdateAppUtil isForce(boolean isForce) {
        this.isForce = isForce;
        return this;
    }

    /**
     * 获取apk的版本号 currentVersionCode
     *
     * @param context
     */
    private void getAppLocalVersion(Context context) {
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            localVersionName = info.versionName;    //版本名
            localVersionCode = info.versionCode;    //版本号

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void update() {

        switch (checkBy) {
            case CHECK_BY_VERSION_CODE:
                if (serverVersionCode > localVersionCode) {//服务版本大于本地版本更新
                    toUpdate();
                } else {
                    Log.i(DownloadAppUtil.LOG_TAG, "当前版本是最新版本" + serverVersionCode + "/" + serverVersionName);
                }
                break;

            case CHECK_BY_VERSION_NAME:
                if (!serverVersionName.equals(localVersionName)) {//服务版本不等于本地版本更新
                    toUpdate();
                } else {
                    Log.i(DownloadAppUtil.LOG_TAG, "当前版本是最新版本" + serverVersionCode + "/" + serverVersionName);
                }
                break;
        }
    }

    private void toUpdate() {
        realUpdate();
    }

    private void realUpdate() {
        ConfirmDialogFragment dialog = ConfirmDialogFragment.newInstance();
    }

    /**
     * 检测wifi是否连接
     */
    private static boolean isWifiConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo networkInfo = cm.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }
        }
        return false;
    }
}
